<?php
/**
 * 供货商管理模型
 * @author cjli
 *
 */
class LiningModel extends Model
{
	/**
	 * 更新供货商
	 * @param array $post post数据
	 * @param int $suppliers_id 供货商ID
	 */
	public static function updateLiningInfo($post, $lining_id=0)
	{
		$lining_db = D('lining');
		$data = array(
			'suppliers_name' => $post['suppliers_name'],
			'breadth' => $post['breadth'],
			'material' => $post['material'],
			'lining_name' => $post['lining_name'],	
			'suppliers_desc' => $post['suppliers_desc'],
			'is_check' => intval($post['is_check']),
			'isspec' => intval($post['isspec']),
			'grooves' => $post['grooves'],
			'admin' => $post['admin'],
		);
		if($lining_id){
			$lining_db->where(array('lining_id' =>$lining_id))->save($data);
		}else{
			$lining_id = $lining_db->add($data);
		}
		S('lining-list', NULL);
		return $lining_id;
	}
	
	/**
	 * 更新供货商与管理员的关联
	 * @param array $admins
	 * @param int $suppliers_id 供货商ID
	 */
	public static function updateLiningAdmin($adminIds, $suppliers_id)
	{
		$admin_db = D('admin');
		$where['id'] = array('in', join(',', $adminIds));
		$admin_db->where(array('suppliers_id' => $suppliers_id))->save(array('suppliers_id' => 0));
		$admin_db->where($where)->save(array('suppliers_id' => $suppliers_id));
	}
	
	/**
	 * 更新供货商跟产品等关联
	 * @param int $suppliers_id 供货商ID
	 */
	public static function updateSuppliersRelation($suppliers_id)
	{
		$product_db = D('product');
	}
	
	/**
	 * 获取供货商列表--用于下拉
	 * Enter description here ...
	 */
	public static function getSuppliersList()
	{
		if(S('suppliers-list')){
			$suppliersList = S('suppliers-list');
		}else{
			$suppliersList = array();
			$supplier_db = D('suppliers');
			$suppliersArray = $supplier_db->field('suppliers_id, suppliers_name')->where('is_check=1')->select();
			if($suppliersArray){
				foreach ($suppliersArray as $supp){
					$suppliersList[$supp['suppliers_id']] = $supp['suppliers_name'];
				}
			}
			S('suppliers-list', $suppliersList);
		}
		return $suppliersList;
	}
	
	/**
	 * 获取供货商详细信息
	 * @param int $suppliers_id 供货商ID
	 */
	public static function getLiningInfo($lining_id)
	{
		$lining_db = D('lining');
		$liningInfo = $lining_db->find($lining_id);
		return $liningInfo;
	}
	
	/**
	 * 删除供货商信息
	 * @param int $suppliers_id 供货商ID
	 */
	public static function deleteLiningInfo($lining_id)
	{
		$lining_db = D('lining');
		$lining_db->delete($lining_id);
		
		$admin_db = D('admin');
		$admin_db->where(array('lining_id' => $lining_id))->save(array('lining_id' => 0));
		$product_db = D('product');
		$product_db->where(array('lining_id' => $lining_id))->save(array('lining_id' => 0));
	}
}